package TopOneHundred;

import java.util.Stack;

/**
 * 检验括号的有效性
 * @author le
 */
public class ValidParenTheses
{
    public static void main(String[] args)
    {
        System.out.println(isValid("()[]{}"));
    }

    public static boolean isValid(String s) {
        char[] arr = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        if (arr.length <= 1){
            return false;
        }
        int index = 1;
        stack.push(arr[0]);
        while(index < arr.length){
            if (stack.size() == 0){
                stack.push(arr[index++]);
                continue;
            }
            if (stack.peek() == '(' && arr[index] == ')'){
                stack.pop();
                index++;
            }
            else if (stack.peek() == '{' && arr[index] == '}'){
                stack.pop();
                index++;
            }
            else if (stack.peek() == '[' && arr[index] == ']'){
                stack.pop();
                index++;
            }
            else{
                stack.push(arr[index]);
                index++;
            }
        }
        return stack.size() == 0;
    }
}
